//
//  MJDIYHeader.m
//  MJRefreshExample
//
//  Created by MJ Lee on 15/6/13.
//  Copyright © 2015年 小码哥. All rights reserved.
//

#import "MJDIYHeader.h"

@interface MJDIYHeader()
@property (weak, nonatomic) UILabel *label;
@property (weak, nonatomic) UIImageView *logo;
@property (weak, nonatomic) UIImageView *arrowView;

@property (weak, nonatomic) UIImageView *loadView;

@property (nonatomic,strong) CABasicAnimation* rotationAnimation;
@end

@implementation MJDIYHeader
#pragma mark - 重写方法
#pragma mark 在这里做一些初始化配置（比如添加子控件）



- (UIImageView *)arrowView
{
    if (!_arrowView) {
        UIImage *image = [UIImage imageNamed:MJRefreshSrcName(@"arrow.png")] ?: [UIImage imageNamed:MJRefreshFrameworkSrcName(@"arrow.png")];
        UIImageView *arrowView = [[UIImageView alloc] initWithImage:image];
        [self addSubview:_arrowView = arrowView];
    }
    return _arrowView;
}

-(UIImageView *)loadView{
    if (!_loadView) {
        UIImage *images = [UIImage imageNamed:MJRefreshSrcName(@"bg.png")] ?: [UIImage imageNamed:MJRefreshFrameworkSrcName(@"bg.png")];
        UIImageView *arrowViews = [[UIImageView alloc] initWithImage:images];
        
        [self addSubview:_loadView = arrowViews];
    }
    return _loadView;
}
- (void)prepare
{
    [super prepare];
    
    // 设置控件的高度
    self.mj_h = 40;
    
    // 添加label
    UILabel *label = [[UILabel alloc] init];
    label.textColor = [UIColor colorWithHexString:@"#06ab9e"];;
    label.font = [UIFont boldSystemFontOfSize:15];
    label.textAlignment = NSTextAlignmentCenter;
    [self addSubview:label];
    self.label = label;
    
}



#pragma mark 在这里设置子控件的位置和尺寸
- (void)placeSubviews
{
    [super placeSubviews];

    self.label.frame = CGRectMake(0,0,120,21);
    self.label.center = CGPointMake(self.mj_w * 0.5, self.mj_h * 0.5);
    self.label.textColor = [UIColor colorWithHexString:@"#06ab9e"];
    // 箭头的中心点
    
    [self.arrowView makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.centerY).with.offset(0);
        make.right.equalTo(self.label.left).with.offset(0);
        make.width.mas_equalTo(self.arrowView.image.size.width);
        make.height.mas_equalTo(self.arrowView.image.size.height);
    }];
    // 圈圈
    if (self.loadView.constraints.count == 0) {
        [self.loadView makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.centerY).with.offset(0);
            make.right.equalTo(self.label.left).with.offset(0);
            make.width.mas_equalTo(self.loadView.image.size.width);
            make.height.mas_equalTo(self.loadView.image.size.height);
        }];
    }
    self.loadView.alpha = 0.0;

//    NSLog(@"self.loadView = %f ,%f",self.arrowView.frame.origin.x ,self.loadView.center.y);
}

#pragma mark 监听scrollView的contentOffset改变
- (void)scrollViewContentOffsetDidChange:(NSDictionary *)change
{
    [super scrollViewContentOffsetDidChange:change];

}

#pragma mark 监听scrollView的contentSize改变
- (void)scrollViewContentSizeDidChange:(NSDictionary *)change
{
    [super scrollViewContentSizeDidChange:change];
    
}

#pragma mark 监听scrollView的拖拽状态改变
- (void)scrollViewPanStateDidChange:(NSDictionary *)change
{
    [super scrollViewPanStateDidChange:change];

}

#pragma mark 监听控件的刷新状态
- (void)setState:(MJRefreshState)state
{
    MJRefreshCheckState
    // 根据状态做事情
    if (state == MJRefreshStateIdle) {
        if (oldState == MJRefreshStateRefreshing) {
            self.arrowView.transform = CGAffineTransformIdentity;
            
            [UIView animateWithDuration:MJRefreshSlowAnimationDuration animations:^{
                self.loadView.alpha = 0.0;
            } completion:^(BOOL finished) {
                // 如果执行完动画发现不是idle状态，就直接返回，进入其他状态
                if (self.state != MJRefreshStateIdle){
                    return;
                }else{
                    [self.loadView.layer removeAllAnimations];
                }
                self.arrowView.hidden = NO;
            }];
        } else {
            self.arrowView.hidden = NO;
            [UIView animateWithDuration:MJRefreshFastAnimationDuration animations:^{
                self.arrowView.transform = CGAffineTransformIdentity;
            }];
        }
        self.label.text = @"下拉可以刷新...";
    } else if (state == MJRefreshStatePulling) {
        self.arrowView.hidden = NO;
        [UIView animateWithDuration:MJRefreshFastAnimationDuration animations:^{
            self.arrowView.transform = CGAffineTransformMakeRotation(0.000001 - M_PI);
        }];
        self.label.text = @"松开即可刷新...";
    } else if (state == MJRefreshStateRefreshing) {
        self.arrowView.hidden = YES;
        self.loadView.alpha = 1.0;
        self.label.text = @"努力加载中...";
        [self loadViewSport];
    }
}

-(void)loadViewSport{

    CABasicAnimation * rotationAnimations = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimations.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
    rotationAnimations.duration = 0.5;
    rotationAnimations.cumulative = YES;
    rotationAnimations.repeatCount = FLT_MAX;

    [self.loadView.layer addAnimation:rotationAnimations forKey:@"rotationAnimation"];
}

#pragma mark 监听拖拽比例（控件被拖出来的比例）
- (void)setPullingPercent:(CGFloat)pullingPercent
{
    [super setPullingPercent:pullingPercent];
    
    self.label.textColor = [UIColor colorWithHexString:@"#06ab9e"];
}

@end
